package com.woniu.communitymanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.communitymanagement.entity.HttpResult;
import com.woniu.communitymanagement.entity.User;
import com.woniu.communitymanagement.entity.vo.UserVO;
import com.woniu.communitymanagement.service.UserService;
import com.woniu.communitymanagement.mapper.UserMapper;
import com.woniu.communitymanagement.util.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
* @author QIQI
* @description 针对表【userinfo(用户表)】的数据库操作Service实现
* @createDate 2022-11-28 10:42:49
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Override
    public HttpResult selectByNameAndPwd(String username, String passwd) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        passwd = Md5.md5(passwd);
        queryWrapper
                .eq("username",username)
                .eq("password",passwd);
        User user = userMapper.selectOne(queryWrapper);
        HttpResult httpResult=null;
        if (user!=null){
            httpResult=new HttpResult(user,null,200,"登陆成功");
            //登陆成功,将用户的id保存到redis中
            Jedis jedis=new Jedis("127.0.0.1",6379);
            jedis.set(user.getId()+"",user.getId()+"");
            jedis.close();//释放资源
        }else {
            httpResult=new HttpResult(null,null,500,"登陆失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult insertOne(UserVO user) {
        User user1 = new User();
        HttpResult httpResult=null;
        user1.setId(null);
        user1.setUsername(user.getUsername());
        user1.setPassword(Md5.md5(user.getPassword()));
        user1.setType(0);
        int count = userMapper.insert(user1);
        if(count>0){
            httpResult=new HttpResult(null,null,200,"注册成功");
        }else{
            httpResult=new HttpResult(null,null,500,"注册失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectAllUserList(Integer pageIndex, Integer pageSize) {
        HttpResult httpResult=null;
        List<User> users = userMapper.selectAllUserList((pageIndex - 1) * pageSize, pageSize);
        int count = userMapper.selectCount();
        if (users!=null&&users.size()>0){
            httpResult=new HttpResult(users,count,200,"查询成功");
        }else {
            if(pageIndex>1){
                users = userMapper.selectAllUserList((pageIndex - 1) * pageSize, pageSize);
                httpResult=new HttpResult(users,count,200,"查询成功");

            }else{
                httpResult=new HttpResult(null,null,500,"查询失败");
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult updateUserById(User user) {
        HttpResult httpResult=null;
        user.setPassword(Md5.md5(user.getPassword()));
        boolean isOk = updateById(user);
        if(isOk){
            httpResult=new HttpResult(null,null,200,"修改成功");
        }else{
            httpResult=new HttpResult(null,null,500,"修改失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult deleteUserById(Integer id) {
        HttpResult httpResult=null;
        boolean isDelete = removeById(id);
        if(isDelete){
            httpResult=new HttpResult(null,null,200,"删除成功");
        }else {
            httpResult=new HttpResult(null,null,500,"删除失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectUserById(Integer id) {
        HttpResult httpResult=null;
        User user = userMapper.selectById(id);
        if(user!=null){
            httpResult=new HttpResult(user,1,200,"查询成功");
        }else{
            httpResult=new HttpResult(null,1,500,"查询失败");
        }
        return httpResult;
    }
}




